#!/usr/bin/env python
# encoding: utf-8
"""
FRCParser.py

Created by Andrew Schreiber on 2009-10-17.
"""

from BeautifulSoup import BeautifulSoup
import re
import string

#We only want certain tags, all others will be removed. Basically this makes our life easier
VALID_TAGS = ['table', 'tr', 'td']

#content is the contents, as a string.
#all other options merely pass them through to the parse_table function.
def parse(content,indexCol = 'Match',skipTitle = False, asInt = False):
	content = re.sub("!","",content)
	tables = sanitize_html(content).findAll('table')
	data = {}
	for i in range(0,len(tables)):
		try:
			table_data = parse_table(tables[i],indexCol,skipTitle,i,asInt)
			data[table_data[0]] = table_data[1]
		except:
			pass
	return data

#Return an associated array of each element in the table
#tData is the table data, it is actually a BeautifulSoup Node
#indexCol specifies the Name of the column that will be used, if none is specified ints will be used.
#skipTitle tells the parser that there is no title on the table. For example Awards has no title but Qualification Schedule does.
#customTitle lets you tell the parser what the title of this table is.
#asInt tells us to cast the indexCol as an Int.
def parse_table(tData,indexCol=None,skipTitle = False, customtitle='Unknown',asInt=False):
	column_names = {}
	tableData = {}
	title = array_to_string(tData.tr.td.contents)
	headercolumns = tData.tr.findNextSibling()
	if(skipTitle):
		headercolumns = tData.findAll('tr')[0]
		title= customtitle
	i = 0
	for col in headercolumns.findAll('td'):
		column_names[i] = array_to_string(col.contents)
		i=i+1
	datacol = headercolumns.findNextSibling()
	index = 0
	while True:
		if (datacol == None):
			break
		column = {}
		i = 0
		for col in datacol.findAll('td'):
			column[column_names[i]] = array_to_string(col.contents)
			i=i+1
		if(indexCol != None):
			if asInt == False:
			  tableData[column[indexCol]] = column
			else:
			  tableData[int(column[indexCol])] = column
		else:
			tableData[index] = column
		datacol = datacol.findNextSibling()
		index = index + 1
	return title,tableData

#Removes all the tags we don't want to see
def sanitize_html(value):
    soup = BeautifulSoup(value)
    for tag in soup.findAll(True):
        if tag.name not in VALID_TAGS:
            tag.hidden = True
    return soup

#strips off blanks
def remove_blank_lines(value):
	return string.strip(value)

#puts the array of things into a nice clean string, makes stuff look pretty.
def array_to_string(value):
	string = ''
	for row in value:
		string+= str(row)
	return remove_blank_lines(string)
